import {createRouter, createWebHistory, RouteLocationNormalizedGeneric, RouteRecordRaw} from 'vue-router';
import {inject} from 'vue';


declare module 'vue-router' {
    interface RouteMeta {
        title: string,
        enterTransition?: string
        leaveTransition?: string
    }
}

//去除url的参数
const removeQueryParams = (to: RouteLocationNormalizedGeneric) => {
    if (Object.keys(to.query).length)
        return {path: to.path, query: {}, hash: to.hash}
}

//去除url的hash
const removeHash = (to: RouteLocationNormalizedGeneric) => {
    if (to.hash) return {path: to.path, query: to.query, hash: ''}
}

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        meta: {
            title: '门户首页'
        },
        component: () => import('@/pages/home.vue'),
        beforeEnter: [removeQueryParams, removeHash],
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '注册登录'
        },
        component: () => import('@/pages/login/index.vue')
    },
    {
        path: '/ca',
        name: 'layout',
        component: () => import('@/layouts/index.vue'),
        redirect: {name: 'caHome'},
        children: [
            {
                path: 'home',
                name: 'caHome',
                meta: {
                    title: '首页',
                    enterTransition: 'animate__fadeIn',
                },
                components: {
                    default: () => import('@/pages/caHome/index.vue'),
                    header: () => import('@/pages/caHome/header.vue'),
                }
            },
            {
                path: 'test',
                name: 'caTest',
                meta: {
                    title: '测试',
                    enterTransition: 'animate__fadeIn'
                },
                component: () => import('@/pages/caTest/index.vue')
            }
        ]
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    const global_router_variable = inject('global_router_variable');
    console.log(global_router_variable, "前置路由守卫", to, from);
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    // a pinia store
    // const userStore = useAuthStore()
    next()
})

router.afterEach((to, from) => {
    console.log("后置路由守卫", to, from);
})

export default router;